//水风险评价函数封装
//构造参数：里程：ZK5+300
// import {
//   DGObject
// } from "./DGObject"
const DGObject = require("./DGObject")
const axios = require("axios")
var sql = require("mssql")
class RiskAssessor {
  constructor(options = {}) {
    this.mile = options.mile
  }
  /**
   * 加载所有风险
   */
  async loadAllRisk(mile) {
    mile = mile ? mile : "ZK5800"
    mile = this.parseMile2Int(mile)
    var queryResult = await this.loadGeoInfo(mile)
    var HydrologyRisk = this.RiskAssessForHydrology(queryResult, mile)

    var CollapseRisk = null
    var ache = queryResult.ache//设计阶段风险
    if (ache != undefined && ache != null) {
      ache = this.getMile(ache)
      mile = ache.PROG_SGJD ? ache.PROG_SGJD : "YK8365"
      var result = this.RiskAssessForCollapse(queryResult, ache)
      return result
    } else {
      CollapseRisk = this.RiskAssessForCollapse_design(queryResult, mile)
    }
    var DeformationRisk = this.RiskAssessForDeformation(queryResult, mile)
    return {
      "CollapseRisk": CollapseRisk,
      "HydrologyRisk": HydrologyRisk,
      "DeformationRisk": DeformationRisk
    }


  }
  /**
   * 突涌水风险评估函数入口,
   * @param {*} mile 里程:不能包含小数，以免无法识别
   */
  async HydrologyRiskAssess(mile) {
    mile = mile ? mile : "ZK5800"
    var queryResult = await this.loadGeoInfo(mile)
    var result = this.RiskAssessForHydrology(queryResult, mile)
    // //console.log("得分为", result)
    return result
  }
  /**
   * 大变形风险评估
   * @param {*} mile 里程：不能包含小数，以免无法识别
   */
  async DeformationAssess(mile) {
    mile = mile ? mile : "YK8365"
    var queryResult = await this.loadGeoInfo(mile)
    var result = this.RiskAssessForDeformation(queryResult, mile)
    // //console.log("得分为", result)
    return result
  }
  //塌方风险评估 输入参数为一个完整的施工进度ache对象：需要评估衬砌的进度信息
  async CollapseRiskAssess(mile) {
    //获取进度信息
    var ache = null
    var ache = await this.asyncLoadGeology(mile, "ache", "construction")
    if (ache != undefined && ache != null) {
      ache = this.getMile(ache)
      mile = ache.PROG_SGJD ? ache.PROG_SGJD : "YK8365"
      var queryResult = await this.loadGeoInfo(mile)
      var result = this.RiskAssessForCollapse(queryResult, ache)
      return result
    } else {
      mile = mile ? mile : "YK8365"
      var queryResult = await this.loadGeoInfo(mile)
      var result = this.RiskAssessForCollapse_design(queryResult, mile)
      return result
    }

  }
  //编写中
  async CollapseRiskAssess_section(section) {
    let PROG_NAME = section.substr(0, 2)
    let PROG_LORR = section.substr(2)
    var conditionSQL = `(([PROG_NAME]='${PROG_NAME}') and ([PROG_LORR]='${PROG_LORR}'))`
    const ache = this.asyncLoadGeology_SQL("construcion", "ache", conditionSQL, 1)
    let mile = ache.PROG_SGJD ? ache.PROG_SGJD : "YK8365"
    var queryResult = await this.loadGeoInfo(mile)
    var result = this.RiskAssessForCollapse(queryResult, ache)
    return result
  }
  /**
   * 突涌水风险评估
   * @param {*} queryResult 
   */
  RiskAssessForHydrology(queryResult, mile) {
    mile = this.parseMile2Int(mile)
    var scoreList = {
      "score1": {
        "score": 0,
        "basis": ""
      },
      "score2": {
        "score": 0,
        "basis": ""
      },
      "score3": {
        "score": 0,
        "basis": ""
      },
      "score4": {
        "score": 0,
        "basis": ""
      },
      "score5": {
        "score": 0,
        "basis": ""
      },
      "score6": {
        "score": 0,
        "basis": ""
      },
      "score7": {
        "score": 0,
        "basis": ""
      },
      "score8": {
        "score": 0,
        "basis": ""
      },
      "score9": {
        "score": 0,
        "basis": ""
      }
    }
    //根据获取到的信息来判断采用何种分析方法
    var Stage = {
      "stageID": 0,
      "StageDescription": "设计"
    }
    if (queryResult.gprf || queryResult.tfsi || queryResult.tspf) {
      Stage.stageID = 1
      Stage.StageDescription = "按实际信息评估"
    } else {
      Stage.stageID = 0
      Stage.StageDescription = "按设计信息评估"
    }

    scoreList.score1 = this.karstAssess(mile)
    var scoreB = queryResult.scoreB
    scoreList.score2.score = scoreB
    if (scoreB == 0) {
      scoreList.score2.basis = "断层不发育"
    } else if (scoreB == 80) {
      scoreList.score2.basis = "断层影响带"
    } else if (scoreB == 100) {
      scoreList.score2.basis = "断层破碎带"
    }
    scoreList.score3 = this.straAssess(queryResult.gprf, queryResult.tspf, queryResult.dgsr, mile)
    scoreList.score4 = {
      "score": 0,
      "basis": "不发育" + "（水勘）"
    }
    scoreList.score5 = this.fragmentationAssess_Hydro(queryResult.gprf, queryResult.tspf, queryResult.stra, mile)
    scoreList.score6 = this.formationAssess(queryResult.gprf, queryResult.tspf, queryResult.dgsr, queryResult.tfsi, queryResult.stra, mile)
    scoreList.score7 = this.leakAssess(queryResult.gprf, queryResult.tspf, queryResult.dgsr)
    scoreList.score8 = this.water(queryResult.gprf, queryResult.tspf, mile)
    scoreList.score9 = this.rockGradeForHydrology(queryResult.gprf, queryResult.tspf, queryResult.dgsr, mile)
    // queryresult:{catagory,scoreB,gprf, tspf, dgsr,skth, tfsi}
    //catagory:风险分类，岩溶类水风险、断层类水风险和无不良地质类水风险三个大类。对应1,2,3

    var weight = []
    var typeName = ""
    //权重
    switch (queryResult.catagory) {
      case 1: {
        weight = [0.07, 0.02, 0.07, 0.02, 0.02, 0.07, 0.24, 0.25, 0.24]
        typeName = "岩溶类"
        break
      }
      case 2: {
        weight = [0.02, 0.07, 0.02, 0.07, 0.07, 0.02, 0.24, 0.25, 0.24]
        typeName = "断层类"
        break
      }
      case 3: {
        weight = [0.02, 0.02, 0.1, 0.1, 0.1, 0.1, 0.19, 0.18, 0.19]
        typeName = "无不良地质类"
        break
      }
      default:
        weight = [0.02, 0.02, 0.1, 0.1, 0.1, 0.1, 0.19, 0.18, 0.19]
        typeName = "无不良地质"
        break
    }
    //console.log("初始得分", scoreList)
    //console.log("权重表", weight)
    var finalScore = this.calScore(scoreList, weight)
    scoreList = this.setDescription(scoreList, weight)
    //console.log(finalScore)
    var risklevel = this.calRiskLevel(finalScore)

    return {
      "finalResult": risklevel[0],
      "stage": Stage,
      "color": risklevel[1],
      "finalScore": finalScore,
      "scoreList": scoreList,
      "weight": weight,
      "type": {
        "ID": queryResult.catagory
      },
      "typeName": typeName
    }
  }
  /**
   * 大变形风险评估
   * @param {*} queryResult 查询结果
   * @returns {} 评估结果
   */
  RiskAssessForDeformation(queryResult, mile) {
    var scoreList = {
      "score1": {
        "score": 0,
        "basis": ""
      },
      "score2": {
        "score": 0,
        "basis": ""
      },
      "score3": {
        "score": 0,
        "basis": ""
      },
      "score4": {
        "score": 0,
        "basis": ""
      },
      "score5": {
        "score": 0,
        "basis": ""
      },
      "score6": {
        "score": 0,
        "basis": ""
      },
      "score7": {
        "score": 0,
        "basis": ""
      }
    }
    //根据获取到的信息来判断采用何种分析方法
    var Stage = {
      "stageID": 0,
      "StageDescription": "设计"
    }
    if (queryResult.gprf || queryResult.tfsi || queryResult.tspf) {
      Stage.stageID = 1
      Stage.StageDescription = "按实际信息评估"
    } else {
      Stage.stageID = 0
      Stage.StageDescription = "按设计信息评估"
    }
    scoreList.score1.score = 0
    scoreList.score1.basis = "-"
    scoreList.score2.score = 50
    scoreList.score2 = this.fragmentationAssess_Deformation(queryResult.gprf, queryResult.tspf, queryResult.stra, mile)
    scoreList.score3 = this.weathering(queryResult.gprf, queryResult.tspf, queryResult.dgsr, queryResult.tfsi, queryResult.stra)
    scoreList.score4 = this.water(queryResult.tfsi, queryResult.tspf, mile)
    scoreList.score5 = {
      "score": 50,
      "basis": "4-7"
    }
    //权重
    switch (queryResult.catagory) {
      case 2: {
        scoreList.score6 = {
          "score": 100,
          "basis": "断层"
        }
        break
      }
      case 3: {
        scoreList.score6 = {
          "score": 0,
          "basis": "无"
        }
        break
      }
      default:
        scoreList.score6 = {
          "score": 0,
          "basis": "无"
        }
        break
    }
    scoreList.score7 = this.weakInterLayer(queryResult.gprf, queryResult.tspf, mile)

    var finalScore = this.calScore_deformation(scoreList)
    scoreList = this.setDescription_deformation(scoreList)
    var risklevel = this.calRiskLevel_deformation(finalScore)

    return {
      "finalResult": risklevel[0],
      "stage": Stage,
      "color": risklevel[1],
      "finalScore": finalScore,
      "scoreList": scoreList
    }
  }
  //获取查询所需的里程信息
  getMile(ache) {
    let rawMile = ache.PROG_SGJD
    var prefix = rawMile.substr(0, 2)
    var mile = parseInt(rawMile.replace(/ZK|YK/, "").replace("+", ""))
    ache.PROG_SGJD = prefix + mile
    rawMile = ache.PROG_CCJD
    prefix = rawMile.substr(0, 2)
    mile = parseInt(rawMile.replace(/ZK|YK/, "").replace("+", ""))
    ache.PROG_CCJD = prefix + mile
    return ache
  }
  //提取数字
  getNum(mile) {
    if (mile) {
      return parseInt(mile.replace(/ZK|YK/, "").replace("+", ""))
    } else return null
  }

  //将里程四舍五入变换为整数
  parseMile2Int(mile) {
    var prefix = mile.substr(0, 2)
    var mileNum = Math.round(mile.replace(/ZK|YK/, "").replace("+", ""))
    var intMile = prefix + mileNum
    return intMile
  }

  //塌方风险评估
  RiskAssessForCollapse(queryResult, ache) {
    var scoreList = {
      "score1": {
        "score": 0,
        "basis": ""
      },
      "score2": {
        "score": 0,
        "basis": ""
      },
      "score3": {
        "score": 0,
        "basis": ""
      },
      "score4": {
        "score": 0,
        "basis": ""
      },
      "score5": {
        "score": 0,
        "basis": ""
      },
      "score6": {
        "score": 0,
        "basis": ""
      },
      "score7": {
        "score": 0,
        "basis": ""
      },
      "score8": {
        "score": 0,
        "basis": ""
      },
      "score9": {
        "score": 0,
        "basis": ""
      }
    }
    //根据获取到的信息来判断采用何种分析方法
    var Stage = {
      "stageID": 0,
      "StageDescription": "设计"
    }
    if (queryResult.gprf || queryResult.tfsi || queryResult.tspf) {
      Stage.stageID = 1
      Stage.StageDescription = "按实际信息评估"
    } else {
      Stage.stageID = 0
      Stage.StageDescription = "按设计信息评估"
    }

    // queryresult:{catagory,scoreB,gprf, tspf, dgsr,skth, tfsi}
    scoreList.score1 = this.rockGradeForCollapse(queryResult.gprf, queryResult.tspf, queryResult.dgsr, mile)
    scoreList.score2 = this.fragmentationAssess(queryResult.gprf, queryResult.tspf, queryResult.stra, ache.PROG_SGJD)
    scoreList.score3 = {
      "score": 0,
      "basis": "大于60m"
    }
    var scoreB = queryResult.scoreB
    if (scoreB == 100) {
      scoreList.score4 = {
        "score": 100,
        "basis": "断层破碎带(设计)"
      }
    } else if (scoreB == 80) {
      scoreList.score4 = {
        "score": 80,
        "basis": "断层影响带"
      }
    }
    else {
      scoreList.score4 = {
        "score": 0,
        "basis": "无"
      }
    }
    scoreList.score5 = {
      "score": 50,
      "basis": "单洞1~2车道"
    }
    //scoreList.score5 = this.stabilityAssess(queryResult.gprf, queryResult.tspf, queryResult.dgsr)
    scoreList.score6 = this.leakAssess_collapse(queryResult.gprf, queryResult.tspf, ache.PROG_SGJD)
    //console.log("地质信息符合性暂无评价标准")
    scoreList.score7 = {
      "score": 0,
      "basis": "符合"
    }
    scoreList.score8 = {
      "score": 0,
      "basis": "符合"
    }
    scoreList.score9 = this.acheAsses(ache, queryResult.gprf, queryResult.tspf, queryResult.dgsr)

    //var weight = [0.2, 0.2, 0.07, 0.07, 0.08, 0.13, 0.08, 0.17 / 3, 0.17 / 3, 0.17 / 3]
    var weight = [0.15, 0.05, 0.05, 0.2, 0.05, 0.1, 0.05, 0.35 / 2, 0.35 / 2]
    var typeName = "塌方风险"
    //权重
    //console.log("初始得分", scoreList)
    //console.log("权重表", weight)
    var finalScore = this.calScore(scoreList, weight)
    scoreList = this.setDescription_Collapse(scoreList, weight)
    for (let x in weight) {
      weight[x] = weight[x].toFixed(2)
    }
    //console.log(finalScore)
    var risklevel = this.calRiskLevel_Collapse(finalScore)
    return {
      "finalResult": risklevel[0],
      "stage": Stage,
      "color": risklevel[1],
      "finalScore": finalScore,
      "scoreList": scoreList,
      "weight": weight,
      "type": {
        "ID": queryResult.catagory
      },
      "typeName": typeName
    }
  }

  // 设计阶段塌方风险评估
  RiskAssessForCollapse_design(queryResult, mile) {
    var scoreList = {
      "score1": {
        "score": 0,
        "basis": ""
      },
      "score2": {
        "score": 0,
        "basis": ""
      },
      "score3": {
        "score": 0,
        "basis": ""
      },
      "score4": {
        "score": 0,
        "basis": ""
      },
      "score5": {
        "score": 0,
        "basis": ""
      },
      "score6": {
        "score": 0,
        "basis": ""
      }
    }
    //根据获取到的信息来判断采用何种分析方法
    var Stage = {
      "stageID": 0,
      "StageDescription": "按设计信息评估"
    }
    // queryresult:{catagory,scoreB,gprf, tspf, dgsr,skth, tfsi}
    scoreList.score1 = this.rockGradeForCollapse(queryResult.gprf, queryResult.tspf, queryResult.dgsr, mile)
    scoreList.score2 = this.fragmentationAssess(queryResult.gprf, queryResult.tspf, queryResult.stra, mile)
    scoreList.score3.score = 40
    scoreList.score3.basis = ">60m"

    scoreList.score5 = {
      "score": 50,
      "basis": "单洞1~2车道"
    }
    var scoreB = queryResult.scoreB
    if (scoreB == 100) {
      scoreList.score4 = {
        "score": 100,
        "basis": "断层破碎带"
      }
    } else if (scoreB == 80) {
      scoreList.score4 = {
        "score": 80,
        "basis": "断层影响带"
      }
    } else {
      scoreList.score4 = {
        "score": 0,
        "basis": "无"
      }
    }
    scoreList.score6 = this.leakAssess_collapse(queryResult.gprf, queryResult.tspf, mile)

    var weight = [0.250, 0.083, 0.083, 0.333, 0.083, 0.167]
    var typeName = "塌方风险"
    //权重
    //console.log("初始得分", scoreList)
    //console.log("权重表", weight)
    var finalScore = this.calScore(scoreList, weight)
    scoreList = this.setDescription_Collapse(scoreList, weight)
    for (let x in weight) {
      weight[x] = weight[x].toFixed(2)
    }
    //console.log(finalScore)
    var risklevel = this.calRiskLevel_Collapse(finalScore)
    return {
      "finalResult": risklevel[0],
      "stage": Stage,
      "color": risklevel[1],
      "finalScore": finalScore,
      "scoreList": scoreList,
      "weight": weight,
      "type": {
        "ID": queryResult.catagory
      },
      "typeName": typeName
    }
  }
  calRiskLevel(finalScore) {
    var risklevel = ""
    var color = ""
    switch (parseInt(finalScore / 20)) {
      case 4:
        risklevel = "极高"
        color = "#FF4500" //红#FF0000
        break
      case 3:
        risklevel = "高"
        color = "#FFA500" //橙#FFA500
        break
      case 2:
        risklevel = "较高"
        color = "#F0E68C" //黄#FFFF00
        break
      case 1:
        risklevel = "中"
        color = "#87CEFA" //淡绿
        break
      case 0:
        risklevel = "低"
        color = "#90EE90" //绿
        break
      default:
        break
    }
    return [risklevel, color]
  }
  calRiskLevel_Collapse(finalScore) {
    var risklevel = ""
    var color = ""
    switch (parseInt(finalScore / 20)) {
      case 4:
        risklevel = "极高"
        color = "#FF4500" //红
        break
      case 3:
        risklevel = "高"
        color = "#FFA500" //橙
        break
      case 2:
        risklevel = "较高"
        color = "#F0E68C" //黄
        break
      case 1:
        risklevel = "低"
        color = "#87CEFA" //亮蓝
        break
      case 0:
        risklevel = "极低"
        color = "#90EE90" //淡绿
        break
      default:
        break
    }
    return [risklevel, color]
  }
  /**
   * 风险等级--大变形
   * @param {*} finalScore 
   */
  calRiskLevel_deformation(finalScore) {
    var risklevel = ""
    var color = ""
    switch (parseInt(finalScore / 250)) {
      case 3:
        risklevel = "极高"
        color = "#FF4500" //红
        break
      case 2:
        risklevel = "高"
        color = "#FFA500" //橙
        break
      case 1:
        risklevel = "较高"
        color = "#F0E68C" //黄
        break
      case 0:
        risklevel = "低"
        color = "#90EE90" //绿
        break
      default:
        break
    }
    return [risklevel, color]
  }
  /**
   * 计算得分-塌方、涌水
   * @param {*} scoreList 
   * @param {*} weight 
   */
  calScore(scoreList, weight) {
    var finalScore = 0
    var index = 0
    for (let x in scoreList) {
      finalScore += scoreList[x].score * weight[index++]
    }
    return finalScore.toFixed(2) //保留两位小数
  }
  /**
   * 计算得分-大变形
   * @param {*} scoreList 
   * @param {*} weight 
   */
  calScore_deformation(scoreList, weight) {
    var finalScore = 0
    var sum = scoreList.score1.score + scoreList.score2.score + scoreList.score3.score + scoreList.score4.score + scoreList.score5.score + scoreList.score6.score
    finalScore = sum * scoreList.score7.score * 1.1
    return finalScore.toFixed(2) //保留两位小数
  }
  //为突涌水风险打分增加说明信息
  setDescription(scoreList, weight) {
    var description = [
      "岩溶发育程度",
      "断层发育情况",
      "地层岩性",
      "周围水体情况",
      "围岩破碎情况",
      "岩层倾角",
      "开挖面涌水情况",
      "地下水发育情况",
      "围岩等级"
    ]
    let i = 0
    for (let x in scoreList) {
      scoreList[x].description = description[i]
      scoreList[x].weight = weight[i++]
    }
    return scoreList
  }

  //为打分增加说明信息--塌方风险
  setDescription_Collapse(scoreList, weight) {
    var description = [
      "围岩等级",
      "围岩破碎情况",
      "隧道埋深",
      "断层破碎带",
      "隧道断面",
      "渗水情况",
      "地质符合性",
      "施工方法",
      "支护间距"
    ]
    let i = 0
    for (let x in scoreList) {
      scoreList[x].description = description[i]
      scoreList[x].weight = weight[i++]
    }
    return scoreList
  }
  //大变形风险--说明信息
  setDescription_deformation(scoreList) {
    var description = [
      "饱和单轴抗压强度",
      "围岩破碎情况",
      "风化程度",
      "地下水发育情况",
      "强度应力比",
      "不良地质分布",
      "特殊矿物"
    ]
    let i = 0
    for (let x in scoreList) {
      scoreList[x].description = description[i++]
    }
    return scoreList
  }

  /**
   * 地下水发育评估
   * @param {*} tfsi 
   * @param {*} tspf 
   */
  water(tfsi, tspf, mile) {
    var waterStr = ""
    var sourceName = ""
    if (tfsi && tfsi.TFSI_GROS) {
      waterStr = tfsi.TFSI_GROS
      sourceName = "(掌子面素描)"
    } else if (tspf && tspf.TPSF_DXS) {
      waterStr = tspf.TPSF_DXS
      sourceName = "(TSP)"
    }
    var mileNum = this.getNum(mile)
    if (waterStr) {
      var pattern1 = /较发育/
      var pattern2 = /不发育/
      var pattern3 = /发育/
      if (pattern1.test(waterStr)) {
        return {
          "score": 50,
          "basis": "较发育" + sourceName
        }
      } else if (pattern2.test(waterStr)) {
        return {
          "score": 0,
          "basis": "不发育" + sourceName
        }
      } else if (pattern3.test(waterStr)) {
        return {
          "score": 100,
          "basis": "发育" + sourceName
        }
      }
    } else if (mileNum > 6150 && mileNum < 7700) {
      return {
        "score": 50,
        "basis": "较发育(设计)"
      }
    } else if (mileNum > 7700 && mileNum < 8000) {
      return {
        "score": 100,
        "basis": "发育(设计)"
      }
    } else if (mileNum > 8000 && mileNum < 8300) {
      return {
        "score": 50,
        "basis": "较发育(设计)"
      }
    } else if (mileNum > 8300 && mileNum < 8600) {
      return {
        "score": 100,
        "basis": "发育(设计)"
      }
    } else if (mileNum > 6150 && mileNum < 6300) {
      return {
        "score": 0,
        "basis": "不发育(缺省值)"
      }
    }
    return {
      "score": 0,
      "basis": "不发育(缺省值)"
    }
  }
  //围岩等级--突涌水风险评估
  rockGradeForHydrology(gprf, tspf, dgsr, mile) {
    var grade = ""
    if (gprf && gprf.GPRF_YBWY) {
      grade = gprf.GPRF_YBWY
      return this.rockGrade_Hydro(grade, "(GPR)")
    } else if (tspf && tspf.TPSF_TCWY) {

      grade = tspf.TPSF_TCWY
      return this.rockGrade_Hydro(grade, "(TSP)")
    } else if (dgsr && dgsr.DGSR_SURR) {
      grade = dgsr.DGSR_SURR
      return this.rockGrade_Hydro(grade, "(设计)")
    } else {
      grade = this.getDesignRockGrade(mile)
      if (grade) {
        return this.rockGrade_Hydro(grade, "(设计)")
      }
    }
    return {
      "score": 75,
      "basis": "V(缺省值)"
    }
  }
  /**
   * 围岩等级打分-突涌水
   */
  rockGrade_Hydro(grade, sourceName) {
    var pattern1 = /Ⅵ|VI/
    var pattern2 = /Ⅴ|V/
    var pattern3 = /Ⅲ|Ⅳ|III|IV/
    if (pattern1.test(grade)) {
      return {
        "score": 100,
        "basis": "VI" + sourceName
      }
    }
    if (pattern3.test(grade)) {
      return {
        "score": 50,
        "basis": pattern3.exec(grade) + sourceName
      }
    }
    if (pattern2.test(grade)) {
      return {
        "score": 75,
        "basis": "V" + sourceName
      }
    } else return {
      "score": 0,
      "basis": grade + sourceName
    }
  }
  /**
   * 根据里程获取设计围岩等级
   * @param {string} mile 
   */
  getDesignRockGrade(mile) {
    var mileNum = this.getNum(mile)
    if (mileNum < 6150 || mileNum > 8600) {
      return 0
    }
    if (mileNum < 6300) {
      return "Ⅴ"
    } else if (mileNum < 7700) {
      return "Ⅳ"
    } else if (mileNum < 8000) {
      return "Ⅲ"
    } else if (mileNum < 8300) {
      return "Ⅳ"
    } else return "Ⅲ"

  }
  /**
   *  围岩等级评估--塌方
   * @param {*} grade 
   * @param {*} sourceName 
   */
  RokcAseess(grade, sourceName) {
    var pattern1 = /Ⅵ|VI/
    var pattern2 = /Ⅴ|V/
    var pattern3 = /Ⅳ|IV/
    var pattern4 = /Ⅲ|III/
    var pattern5 = /Ⅱ|II/
    var pattern6 = /Ⅰ|I/
    if (pattern1.test(grade)) {
      return {
        "score": 100,
        "basis": "VI" + "(" + sourceName + ")"
      }
    }
    if (pattern3.test(grade)) {
      return {
        "score": 80,
        "basis": "Ⅳ" + "(" + sourceName + ")"
      }
    }
    if (pattern2.test(grade)) {
      return {
        "score": 90,
        "basis": "V" + "(" + sourceName + ")"
      }
    }
    if (pattern4.test(grade)) {
      return {
        "score": 50,
        "basis": "Ⅲ" + "(" + sourceName + ")"
      }

    }
    if (pattern5.test(grade)) {
      return {
        "score": 25,
        "basis": "Ⅱ" + "(" + sourceName + ")"
      }
    }
    if (pattern6.test(grade)) {
      return {
        "score": 0,
        "basis": "Ⅰ" + "(" + sourceName + ")"
      }
    } else return {
      "score": 100,
      "basis": "格式有误" + "(" + sourceName + "):" + grade
    }

  }
  /**
   * 围岩分级打分---塌方风险
   * @param {*} gprf 
   * @param {*} tspf 
   * @param {*} dgsr 
   */
  rockGradeForCollapse(gprf, tspf, dgsr, mile) {
    if (gprf) {
      if (gprf.GPRF_YBWY) {
        let tmp = gprf.GPRF_YBWY
        return this.RokcAseess(tmp, "GPR")
      }
    } else if (tspf) {
      if (tspf.TPSF_TCWY) {
        let tmp = tspf.TPSF_TCWY
        return this.RokcAseess(tmp, "TSP")
      }
    } else if (dgsr) {
      if (dgsr.DGSR_SURR) {
        let tmp = dgsr.DGSR_SURR
        return this.RokcAseess(tmp, "设计")
      }
    } else {
      let grade = this.getDesignRockGrade(mile)
      if (grade) {
        return this.RokcAseess(grade, "设计")
      }
    }

  }
  //施工因素评估
  acheAsses(ache, gprf, tspf, dgsr) {
    let rockGrade = ""
    if (gprf) {
      if (gprf.GPRF_YBWY) {
        rockGrade = gprf.GPRF_YBWY
      }
    } else if (tspf) {
      if (tspf.TPSF_TCWY) {
        rockGrade = tspf.TPSF_TCWY
      }
    } else if (dgsr) {
      if (dgsr.DGSR_SURR) {
        rockGrade = dgsr.DGSR_SURR
      }
    }
    if (!ache.PROG_SGJD) {
      return {
        "score": 100,
        "basis": "进度数据有误"
      }
    }
    let PROG_SGJD = this.getNum(ache.PROG_SGJD) //掌子面里程
    let PROG_CCJD = this.getNum(ache.PROG_CCJD) //初衬里程
    let diff = Math.abs(PROG_SGJD - PROG_CCJD)
    if (diff > 1000) {
      return {
        "score": 100,
        "basis": "进度数据有误"
      }
    }
    var pattern1 = /Ⅴ|V|Ⅳ|IV/
    var pattern2 = /Ⅲ|III|Ⅱ|II|Ⅰ|I/
    if (pattern1.test(rockGrade)) {

      if (diff < 70) {
        return {
          "score": 0,
          "basis": "及时" + diff + "m"
        }
      } else if (diff < 120) {
        return {
          "score": 50,
          "basis": "滞后" + diff + "m"
        }
      } else if (diff < 200) {
        return {
          "score": 75,
          "basis": "滞后" + diff + "m"
        }
      } else if (diff > 200) {
        return {
          "score": 100,
          "basis": "严重滞后"
        }
      }
    } else if (pattern2.test(rockGrade)) {
      return {
        "score": 0,
        "basis": "及时"
      }
    } else {
      return {
        "score": 100,
        "basis": "缺少数据"
      }
    }
  }
  /**
   * 断层风险评估
   */
  faultAssess(mile) {
    var mileNum = this.getNum(mile)


  }
  //地下水状态
  leakAssess(gprf, tspf, dgsr, tfsi) {
    if (gprf) {
      if (gprf.GPRF_DXSDJ.length > 1) {
        var tmp = gprf.GPRF_DXSDJ
        //使用正则表达式，如果含有该字段，则返回true 
        var pattern1 = /大股涌水/
        var pattern2 = /小股涌水/
        var pattern3 = /淋雨状/
        var pattern4 = /点滴/
        var pattern5 = /潮湿/
        var pattern6 = /稍湿/
        if (pattern1.test(tmp)) {
          return {
            "score": 100,
            "basis": "大股涌水" + "(GPR)"
          }
        } else if (pattern2.test(tmp)) {
          return {
            "score": 80,
            "basis": "小股涌水" + "(GPR)"
          }
        } else if (pattern3.test(tmp)) {
          return {
            "score": 60,
            "basis": "淋雨状" + "(GPR)"
          }
        } else if (pattern4.test(tmp)) {
          return {
            "score": 40,
            "basis": "点滴" + "(GPR)"
          }
        } else if (pattern5.test(tmp)) {
          return {
            "score": 20,
            "basis": "潮湿" + "(GPR)"
          }
        } else if (pattern6.test(tmp)) {
          return {
            "score": 10,
            "basis": "稍湿" + "(GPR)"
          }
        } else if (tmp.search("干燥") || tmp.search("无水")) {
          return {
            "score": 0,
            "basis": "干燥" + "(GPR)"
          }
        } else return {
          "score": 20,
          "basis": "潮湿" + "(缺省值)"
        }
      } else return {
        "score": 20,
        "basis": "潮湿" + "(缺省值)"
      }
    }

    if (tspf) {
      if (tspf.TPSF_DXS.length > 2) {
        var tmp = tspf.TPSF_DXS
        //使用正则表达式，如果含有该字段，则返回true 
        var pattern1 = /大股涌水/
        var pattern2 = /小股涌水/
        var pattern3 = /淋雨状/
        var pattern4 = /点滴/
        var pattern5 = /潮湿/
        var pattern6 = /稍湿/
        var pattern7 = /干燥/
        if (pattern1.test(tmp)) {
          return {
            "score": 100,
            "basis": "大股涌水" + "(TSP)"
          }
        } else if (pattern2.test(tmp)) {
          return {
            "score": 100,
            "basis": "小股涌水" + "(TSP)"
          }
        } else if (pattern3.test(tmp)) {
          return {
            "score": 80,
            "basis": "淋雨状" + "(TSP)"
          }
        } else if (pattern4.test(tmp)) {
          return {
            "score": 60,
            "basis": "点滴" + "(TSP)"
          }
        } else if (pattern5.test(tmp)) {
          return {
            "score": 40,
            "basis": "潮湿" + "(TSP)"
          }
        } else if (pattern6.test(tmp)) {
          return {
            "score": 20,
            "basis": "稍湿" + "(TSP)"
          }
        } else if (pattern7.test(tmp) || tmp.search("无水")) {
          return {
            "score": 10,
            "basis": "干燥" + "(TSP)"
          }
        } else return {
          "score": 100,
          "basis": "格式有误" + "(TSP)"
        }
      } else return {
        "score": 20,
        "basis": "潮湿" + "(缺省值)"
      }
    } else return {
      "score": 20,
      "basis": "潮湿(缺省值)"
    }
  }
  //地下水状态-塌方风险
  leakAssess_collapse(gprf, tspf) {
    if (gprf) {
      if (gprf.GPRF_DXSDJ.length > 1) {
        var tmp = gprf.GPRF_DXSDJ
        return this.leakAssess_el(tmp, "GPR")
      }
    }
    if (tspf) {
      if (tspf.TPSF_DXS.length > 2) {
        var tmp = tspf.TPSF_DXS
        return this.leakAssess_el(tmp, "TSP")
      }
    } else return this.leakAssess_el("潮湿", "设计")

  }
  //地下水状态具体打分--塌方风险
  leakAssess_el(str, sourceName) {
    var pattern1 = /大股涌水/
    var pattern2 = /小股涌水/
    var pattern3 = /淋雨状|涌流/
    var pattern4 = /点滴/
    var pattern5 = /潮湿/
    var pattern6 = /稍湿/
    var pattern7 = /干燥/
    if (pattern1.test(str)) {
      return {
        "score": 75,
        "basis": "大股涌水" + "(" + sourceName + ")"
      }
    } else if (pattern2.test(str)) {
      return {
        "score": 75,
        "basis": "小股涌水" + "(" + sourceName + ")"
      }
    } else if (pattern3.test(str)) {
      return {
        "score": 75,
        "basis": "淋雨状" + "(" + sourceName + ")"
      }
    } else if (pattern4.test(str)) {
      return {
        "score": 50,
        "basis": "点滴" + "(" + sourceName + ")"
      }
    } else if (pattern5.test(str)) {
      return {
        "score": 25,
        "basis": "潮湿" + "(" + sourceName + ")"
      }
    } else if (pattern6.test(str)) {
      return {
        "score": 25,
        "basis": "稍湿" + "(" + sourceName + ")"
      }
    } else if (pattern7.test(str) || (str.search("无水") + 1)) {
      return {
        "score": 0,
        "basis": "干燥" + "(" + sourceName + ")"
      }
    } else return {
      "score": 100,
      "basis": "缺少本条目" + "(" + sourceName + ")"
    }

  }
  //岩层产状评估
  formationAssess(gprf, tspf, dgsr, tfsi, stra, mile) {


    var str = "124°∠69°"
    var degree = -1
    if (tfsi && tfsi.TFSI_ATTD) {
      str = tfsi.TFSI_ATTD
      var index = str.search("∠")
      if (index) {
        degree = str.substr(index).replace(/[^0-9]/ig, "")
        return this.assessFormation(degree)
      }
    } else if (stra && stra.STRA_DIR) {
      degree = parseFloat(stra.STRA_DIR)
      return this.assessFormation(degree)
    } else {
      var mileNum = this.getNum(mile)
      if (mileNum > 6150 && mileNum < 8000) {
        return {
          "score": 100,
          "basis": "38（设计）"
        }
      } else if (mileNum > 8000 && mileNum < 8600) {
        return {
          "score": 30,
          "basis": "67（设计）"
        }
      }
      return {
        "score": 0,
        "basis": "无数据"
      }
    }
    return {
      "score": 0,
      "basis": "无数据"
    }

  }
  assessFormation(degree) {
    if (25 < degree && 65 > degree) {
      return {
        "score": 100,
        "basis": degree
      }
    } else if (degree >= 90 || degree <= 0) {
      //console.log("GPR 岩层产状格式有误")
      return {
        "score": 100,
        "basis": degre + "(数值有误)"
      }
    } else if (10 > degree && 0 < degree) {
      return {
        "score": 0,
        "basis": degree
      }
    } else return {
      "score": 30,
      "basis": degree
    }
  }
  /**岩石岩体破碎情况评估 */
  fragmentationAssess(gprf, tspf, stra, mile) {
    var mileNum = this.getNum(mile)
    if (gprf) {
      if (gprf.GPRF_WDX.length > 0) {
        var JGGZ = gprf.GPRF_JGGZ
        var wdx = gprf.GPRF_WDX
        //search返回当前字符的位置，若无结果，返回-1
        if ((JGGZ.search("极破碎") + 1) || ((wdx.search("完整性") + 1) && wdx.search("很差") + 1)) {
          return {
            "score": 100,
            "basis": "极破碎" + "(GPR)"
          }
        } else if ((JGGZ.search("较破碎") + 1) || ((wdx.search("完整性") + 1) && wdx.search("较差") + 1)) {
          return {
            "score": 50,
            "basis": "较破碎" + "(GPR)"
          }
        } else if ((JGGZ.search("破碎") + 1) || ((wdx.search("完整性") + 1) && wdx.search("差") + 1)) {
          return {
            "score": 75,
            "basis": "破碎" + "(GPR)"
          }
        } else if ((JGGZ.search("较完整") + 1) || ((wdx.search("完整性") + 1) && wdx.search("一般") + 1)) {
          return {
            "score": 25,
            "basis": "较完整" + "(GPR)"
          }
        } else if ((JGGZ.search("完整") + 1) || ((wdx.search("完整性") + 1) && wdx.search("较好") + 1)) {
          return {
            "score": 0,
            "basis": "完整" + "(GPR)"
          }
        }
      }
    } else if (tspf) {
      if (tspf.TPSF_WZX.length > 3) {
        var wzx = tspf.TPSF_WZX
        var pattern1 = /极破碎/
        var pattern2 = /较破碎/
        var pattern3 = /破碎/
        var pattern4 = /较完整/
        var pattern5 = /完整/

        if (pattern1.test(wzx)) {
          return {
            "score": 100,
            "basis": "极破碎" + "(TSP)"
          }
        } else if (pattern2.test(wzx)) {
          return {
            "score": 50,
            "basis": "较破碎" + "(TSP)"
          }
        } else if (pattern3.test(wzx)) {
          return {
            "score": 75,
            "basis": "破碎" + "(TSP)"
          }
        } else if (pattern4.test(wzx)) {
          return {
            "score": 25,
            "basis": "较完整" + "(TSP)"
          }
        } else if (pattern5.test(wzx)) {
          return {
            "score": 0,
            "basis": "完整" + "(TSP)"
          }
        }
      }
    } else if (stra && stra.STRA_DESC) {
      let wzx = stra.STRA_DESC
      var pattern1 = /极破碎/
      var pattern2 = /较破碎/
      var pattern3 = /破碎/
      var pattern4 = /较完整/
      var pattern5 = /完整/
      if (pattern1.test(wzx)) {
        return {
          "score": 100,
          "basis": "极破碎" + "(设计)"
        }
      } else if (pattern2.test(wzx)) {
        return {
          "score": 50,
          "basis": "较破碎" + "(设计)"
        }
      } else if (pattern3.test(wzx)) {
        return {
          "score": 75,
          "basis": "破碎" + "(设计)"
        }
      } else if (pattern4.test(wzx)) {
        return {
          "score": 25,
          "basis": "较完整" + "(设计)"
        }
      } else if (pattern5.test(wzx)) {
        return {
          "score": 0,
          "basis": "完整" + "(设计)"
        }
      }
    } else if (mileNum > 6150 && mileNum < 6300) {
      return {
        "score": 100,
        "basis": "松散结构(设计)"
      }
    } else if (mileNum > 6300 && mileNum < 8600) {
      return {
        "score": 50,
        "basis": "镶嵌结构(设计)"
      }
    } else return {
      "score": 75,
      "basis": "破碎(缺省值)"
    }
    return {
      "score": 75,
      "basis": "破碎(缺省值)"
    }
  }
  /**
   * 岩体破碎情况评估--突涌水
   * @param {*} gprf 
   * @param {*} tspf 
   * @param {*} stra 
   * @param {*} mile 
   */
  fragmentationAssess_Hydro(gprf, tspf, stra, mile) {
    var mileNum = this.getNum(mile)
    if (gprf) {
      if (gprf.GPRF_WDX.length > 0) {
        var JGGZ = gprf.GPRF_JGGZ
        var wdx = gprf.GPRF_WDX
        //search返回当前字符的位置，若无结果，返回-1
        if ((JGGZ.search("极破碎") + 1) || ((wdx.search("完整性") + 1) && wdx.search("很差") + 1)) {
          return {
            "score": 100,
            "basis": "极破碎" + "(GPR)"
          }
        } else if ((JGGZ.search("较破碎") + 1) || ((wdx.search("完整性") + 1) && wdx.search("较差") + 1)) {
          return {
            "score": 50,
            "basis": "较破碎" + "(GPR)"
          }
        } else if ((JGGZ.search("破碎") + 1) || ((wdx.search("完整性") + 1) && wdx.search("差") + 1)) {
          return {
            "score": 75,
            "basis": "破碎" + "(GPR)"
          }
        } else if ((JGGZ.search("较完整") + 1) || ((wdx.search("完整性") + 1) && wdx.search("一般") + 1)) {
          return {
            "score": 20,
            "basis": "较完整" + "(GPR)"
          }
        } else if ((JGGZ.search("完整") + 1) || ((wdx.search("完整性") + 1) && wdx.search("较好") + 1)) {
          return {
            "score": 10,
            "basis": "完整" + "(GPR)"
          }
        }
      }
    } else if (tspf) {
      if (tspf.TPSF_WZX.length > 3) {
        var wzx = tspf.TPSF_WZX
        var pattern1 = /极破碎/
        var pattern2 = /较破碎/
        var pattern3 = /破碎/
        var pattern4 = /较完整/
        var pattern5 = /完整/

        if (pattern1.test(wzx)) {
          return {
            "score": 100,
            "basis": "极破碎" + "(TSP)"
          }
        } else if (pattern2.test(wzx)) {
          return {
            "score": 50,
            "basis": "较破碎" + "(TSP)"
          }
        } else if (pattern3.test(wzx)) {
          return {
            "score": 75,
            "basis": "破碎" + "(TSP)"
          }
        } else if (pattern4.test(wzx)) {
          return {
            "score": 20,
            "basis": "较完整" + "(TSP)"
          }
        } else if (pattern5.test(wzx)) {
          return {
            "score": 10,
            "basis": "完整" + "(TSP)"
          }
        }
      }
    } else if (stra && stra.STRA_DESC) {
      let wzx = stra.STRA_DESC
      var pattern1 = /极破碎/
      var pattern2 = /较破碎/
      var pattern3 = /破碎/
      var pattern4 = /较完整/
      var pattern5 = /完整/
      if (pattern1.test(wzx)) {
        return {
          "score": 100,
          "basis": "极破碎" + "(设计)"
        }
      } else if (pattern2.test(wzx)) {
        return {
          "score": 50,
          "basis": "较破碎" + "(设计)"
        }
      } else if (pattern3.test(wzx)) {
        return {
          "score": 75,
          "basis": "破碎" + "(设计)"
        }
      } else if (pattern4.test(wzx)) {
        return {
          "score": 20,
          "basis": "较完整" + "(设计)"
        }
      } else if (pattern5.test(wzx)) {
        return {
          "score": 10,
          "basis": "完整" + "(设计)"
        }
      }
    } else if (mileNum > 6150 && mileNum < 6300) {
      return {
        "score": 100,
        "basis": "松散结构(设计)"
      }
    } else if (mileNum > 6300 && mileNum < 8600) {
      return {
        "score": 50,
        "basis": "镶嵌结构(设计)"
      }
    } else return {
      "score": 75,
      "basis": "破碎(缺省值)"
    }
    return {
      "score": 75,
      "basis": "破碎(缺省值)"
    }
  }
  /**
   * 岩体破碎情况评估-大变形
   * @param {*} gprf 
   * @param {*} tspf 
   * @param {*} stra 
   * @param {*} mile 
   */
  fragmentationAssess_Deformation(gprf, tspf, stra, mile) {
    var mileNum = this.getNum(mile)
    if (gprf) {
      if (gprf.GPRF_WDX.length > 0) {
        var JGGZ = gprf.GPRF_JGGZ
        var wdx = gprf.GPRF_WDX
        //search返回当前字符的位置，若无结果，返回-1
        if ((JGGZ.search("极破碎") + 1) || ((wdx.search("完整性") + 1) && wdx.search("很差") + 1)) {
          return {
            "score": 100,
            "basis": "极破碎" + "(GPR)"
          }
        } else if ((JGGZ.search("较破碎") + 1) || ((wdx.search("完整性") + 1) && wdx.search("较差") + 1)) {
          return {
            "score": 75,
            "basis": "较破碎" + "(GPR)"
          }
        } else if ((JGGZ.search("破碎") + 1) || ((wdx.search("完整性") + 1) && wdx.search("差") + 1)) {
          return {
            "score": 100,
            "basis": "破碎" + "(GPR)"
          }
        } else if ((JGGZ.search("较完整") + 1) || ((wdx.search("完整性") + 1) && wdx.search("一般") + 1)) {
          return {
            "score": 50,
            "basis": "较完整" + "(GPR)"
          }
        } else if ((JGGZ.search("完整") + 1) || ((wdx.search("完整性") + 1) && wdx.search("较好") + 1)) {
          return {
            "score": 0,
            "basis": "完整" + "(GPR)"
          }
        }
      }
    } else if (tspf) {
      if (tspf.TPSF_WZX.length > 3) {
        var wzx = tspf.TPSF_WZX
        var pattern1 = /极破碎/
        var pattern2 = /较破碎/
        var pattern3 = /破碎/
        var pattern4 = /较完整/
        var pattern5 = /完整/

        if (pattern1.test(wzx)) {
          return {
            "score": 100,
            "basis": "极破碎" + "(TSP)"
          }
        } else if (pattern2.test(wzx)) {
          return {
            "score": 75,
            "basis": "较破碎" + "(TSP)"
          }
        } else if (pattern3.test(wzx)) {
          return {
            "score": 100,
            "basis": "破碎" + "(TSP)"
          }
        } else if (pattern4.test(wzx)) {
          return {
            "score": 50,
            "basis": "较完整" + "(TSP)"
          }
        } else if (pattern5.test(wzx)) {
          return {
            "score": 0,
            "basis": "完整" + "(TSP)"
          }
        }
      }
    } else if (stra && stra.STRA_DESC) {
      let wzx = stra.STRA_DESC
      var pattern1 = /极破碎/
      var pattern2 = /较破碎/
      var pattern3 = /破碎/
      var pattern4 = /较完整/
      var pattern5 = /完整/
      if (pattern1.test(wzx)) {
        return {
          "score": 100,
          "basis": "极破碎" + "(设计)"
        }
      } else if (pattern2.test(wzx)) {
        return {
          "score": 75,
          "basis": "较破碎" + "(设计)"
        }
      } else if (pattern3.test(wzx)) {
        return {
          "score": 100,
          "basis": "破碎" + "(设计)"
        }
      } else if (pattern4.test(wzx)) {
        return {
          "score": 50,
          "basis": "较完整" + "(设计)"
        }
      } else if (pattern5.test(wzx)) {
        return {
          "score": 0,
          "basis": "完整" + "(设计)"
        }
      }
    } else if (mileNum > 6150 && mileNum < 6300) {
      return {
        "score": 100,
        "basis": "松散结构(设计)"
      }
    } else if (mileNum > 6300 && mileNum < 8600) {
      return {
        "score": 75,
        "basis": "镶嵌结构(设计)"
      }
    } else return {
      "score": 100,
      "basis": "破碎(缺省值)"
    }
    return {
      "score": 100,
      "basis": "破碎(缺省值)"
    }
  }
  //围岩稳定性-塌方风险评估
  stabilityAssess(gprf, tspf, dgsr) {
    if (gprf) {
      if (gprf.GPRF_WDX.length > 1) {
        var tmp = gprf.GPRF_WDX
        var pattern1 = /稳定|自稳/
        if (pattern1.test(tmp) & tmp.search("很差") + 1) {
          return {
            "score": 100,
            "basis": "很差" + "(GPR)"
          }
        } else if (pattern1.test(tmp) & tmp.search("较差") + 1) {
          return {
            "score": 50,
            "basis": "较差" + "(GPR)"
          }
        } else if (pattern1.test(tmp) & tmp.search("差") + 1) {
          return {
            "score": 75,
            "basis": "差" + "(GPR)"
          }
        } else if (pattern1.test(tmp) & tmp.search("一般") + 1) {
          return {
            "score": 25,
            "basis": "一般" + "(GPR)"
          }
        } else if (pattern1.test(tmp) & tmp.search("较好") + 1) {
          return {
            "score": 10,
            "basis": "较好" + "(GPR)"
          }
        } else return {
          "score": 100,
          "basis": "格式错误" + "(GPR)"
        }
      }
    }
    if (tspf) {
      if (tspf.GPRF_WDX.length > 1) {
        var tmp = tspf.TPSF_WDX
        var pattern1 = /稳定|自稳/
        if (pattern1.test(tmp) & tmp.search("很差") + 1) {
          return {
            "score": 100,
            "basis": "很差" + "(TSP)"
          }
        } else if (pattern1.test(tmp) & tmp.search("较差") + 1) {
          return {
            "score": 50,
            "basis": "较差" + "(TSP)"
          }
        } else if (pattern1.test(tmp) & tmp.search("差") + 1) {
          return {
            "score": 75,
            "basis": "差" + "(TSP)"
          }
        } else if (pattern1.test(tmp) & tmp.search("一般") + 1) {
          return {
            "score": 25,
            "basis": "一般" + "(TSP)"
          }
        } else if (pattern1.test(tmp) & tmp.search("较好") + 1) {
          return {
            "score": 10,
            "basis": "较好" + "(TSP)"
          }
        } else return {
          "score": 100,
          "basis": "暂无" + "(TSP)"
        }
      }
    } else if (dgsr && dgsr.DGSR_STAB) {
      let tmp = dgsr.DGSR_STAB
      var pattern1 = /易坍塌/
      var pattern2 = /无支护时会掉块/
      if (pattern1.test(tmp)) {
        return {
          "score": 50,
          "basis": "较差" + "(设计)"
        }
      } else if (pattern2.test(tmp)) {
        return {
          "score": 25,
          "basis": "一般" + "(设计)"
        }
      }
    } else return {
      "score": 100,
      "basis": "暂无数据"
    }


  }
  /**
   * 岩溶评估、待更新
   * @param {*} gprf 
   * @param {*} mile 
   */
  karstAssess(mile) {
    var mileNum = this.getNum(mile)
    if (mileNum > 7700 && mileNum < 8000) {
      return {
        "score": 100,
        "basis": "岩溶发育(设计)"
      }
    } else return {
      "score": 0,
      "basis": "不发育(设计)"
    }
  }
  /**风化程度
   */
  weathering(gprf, tspf, dgsr, tfsi, stra) {
    var weatheringStr = ""
    var sourceName = ""
    if (gprf && gprf.GRPF_FHCD) {
      weatheringStr += gprf.GRPF_FHCD
      sourceName = "(GPR)"
    } else if (dgsr && dgsr.DGSR_WEA) {
      weatheringStr += dgsr.DGSR_WEA
      sourceName += "(设计)"
    } else if (stra && stra.STRA_WEA) {
      weatheringStr += stra.STRA_WEA
      sourceName = "(设计)"
    } else if (tspf && tspf.TSPF_FHCD) {
      weatheringStr += tspf.TSPF_FHCD
      sourceName = "(TSP)"
    } else if (tfsi && tfsi.TFSI_DEGW) {
      weatheringStr += tfsi.TFSI_DEGW
      sourceName += "(掌子面素描)"
    }
    if (weatheringStr) {

      var pattern1 = /全/
      var pattern2 = /强/
      var pattern3 = /中/
      var pattern4 = /微/
      if (pattern1.test(weatheringStr)) {
        return {
          "score": 100,
          "basis": "全风化" + sourceName
        }
      } else if (pattern2.test(weatheringStr)) {
        return {
          "score": 75,
          "basis": "强风化" + sourceName
        }
      } else if (pattern3.test(weatheringStr)) {
        return {
          "score": 50,
          "basis": "中风化" + sourceName
        }
      } else if (pattern4.test(weatheringStr)) {
        return {
          "score": 0,
          "basis": "微风化" + sourceName
        }
      }
    }
    return {
      "score": 50,
      "basis": "中风化"
    }

  }
  /**
   * 地层岩性
   * @param {*} gprf 
   * @param {*} tspf 
   * @param {*} dgsr 
   * @param  mile 
   */
  straAssess(gprf, tspf, dgsr, mile) {
    var mileNum = this.getNum(mile)
    if (gprf) {
      if (gprf.GPRF_YX) {
        var pattern1 = /石灰岩|白云岩|泥灰岩|泥质岩/
        var pattern3 = /砂岩|页岩|花岗岩/
        var tmp = gprf.GPRF_YX
        if (pattern1.exec(tmp)) {
          return {
            "score": 100,
            "basis": pattern1.exec(tmp)[0] + "(GPR)"
          }
        } else if (pattern3.exec(tmp)) {
          return {
            "score": 0,
            "basis": pattern3.exec(tmp)[0] + "(GPR)"
          }
        } else {
          return {
            "score": 0,
            "basis": "不可溶性围岩(GPR)"
          }
        }
      } else {
        return {
          "score": 0,
          "basis": "不可溶性围岩(GPR)"
        }
      }
    } else if (tspf) {
      if (tspf.TPSF_YX) {
        var pattern1 = /石灰岩|白云岩|泥灰岩|泥质岩/
        var pattern3 = /砂岩|页岩|花岗岩/
        var tmp = tspf.TPSF_YX
        if (pattern1.exec(tmp)) {
          return {
            "score": 100,
            "basis": pattern1.exec(tmp)[0] + "(TSP)"
          }
        } else if (pattern3.exec(tmp)) {
          return {
            "score": 0,
            "basis": pattern3.exec(tmp)[0] + "(TSP)"
          }
        } else {
          return {
            "score": 0,
            "basis": "不可溶性围岩(TSP)"
          }
        }
      } else {
        return {
          "score": 0,
          "basis": "不可溶性围岩(TSP)"
        }
      }

    } else if (dgsr && dgsr.DGSR_LITH) {
      let tmp = dgsr.DGSR_LITH
      var pattern1 = /石灰岩|白云岩|泥灰岩|泥质岩/
      var pattern2 = /泥质粉砂岩/
      var pattern3 = /砂岩|页岩|花岗岩/
      if (pattern1.exec(tmp)) {
        return {
          "score": 100,
          "basis": pattern1.exec(tmp)[0] + "(设计)"
        }
      } else if (pattern3.exec(tmp)) {
        return {
          "score": 0,
          "basis": pattern3.exec(tmp)[0] + "(设计)"
        }
      } else {
        return {
          "score": 0,
          "basis": "不可溶性围岩(设计)"
        }
      }
    } else
      if (mileNum > 6150 && mileNum < 6300) {
        return {
          "score": 100,
          "basis": "碎裂岩、糜棱岩（设计）"
        }
      } else if (mileNum > 6300 && mileNum < 7700) {
        return {
          "score": 50,
          "basis": "灰岩、粉砂岩（设计）"
        }
      } else if (mileNum > 7700 && mileNum < 8000) {
        return {
          "score": 0,
          "basis": "灰岩、页岩（设计）"
        }
      } else if (mileNum > 8000 && mileNum < 8300) {
        return {
          "score": 75,
          "basis": "粉砂岩夹泥灰岩、灰岩（设计）"
        }
      } else if (mileNum > 8300 && mileNum < 8600) {
        return {
          "score": 75,
          "basis": "粉砂岩夹泥灰岩、灰岩（设计）"
        }
      }
    return {
      "score": 0,
      "basis": "暂无数据"
    }
  }
  /**
   * 软弱夹层评估
   * @param {*} gprf 
   * @param {*} tspf 
   * @param {*} tfsi 
   */
  weakInterLayer(gprf, tspf, mile) {
    var descriptionStr = ""
    var flag = 0
    if (tspf) {
      if (tspf.TPSF_YBJGMS) {
        descriptionStr = descriptionStr + tspf.TPSF_YBJGMS
        flag = 1
      }
      if (tspf.TPSF_YX) {
        descriptionStr = descriptionStr + tspf.TPSF_YX
        flag = 1
      }
    }

    if (gprf) {
      if (gprf.GPRF_JGGZ) {
        descriptionStr = descriptionStr + gprf.GPRF_JGGZ
        flag = 1
      }
      if (gprf.GPRF_WDX) {
        descriptionStr = descriptionStr + gprf.GPRF_WDX
        flag = 1
      }
      if (gprf.GPRF_DZLDMS) {
        descriptionStr = descriptionStr + gprf.GPRF_DZLDMS
        flag = 1
      }
      if (gprf.GPRF_YX) {
        descriptionStr = descriptionStr + gprf.GPRF_YX
        flag = 1
      }
    }
    if (flag) {
      var pattern0 = /高岭|蒙脱/
      if (pattern0.test(descriptionStr)) {
        return {
          "score": 2,
          "basis": "含"
        }
      } else {
        return {
          "score": 1,
          "basis": "不含"
        }
      }
    } else {
      return {
        "score": 1,
        "basis": "不含(设计)"
      }
    }
  }
  //测试用--服务宕机模拟
  async loadGeoInfo_test(mile) {

    var result = {
      "catagory": null,
      "scoreB": null,
      "gprf": null,
      "tspf": null,
      "dgsr": null,
      "tfsi": null,
      "stra": null
      // "skth": skth,
      // "tfsi": tfsi
    }
    //console.log("rawdata loaded" + result)
    return result
  }
  //获取评估资料，mile为ZK8325形式
  async loadGeoInfo(mile) {
    mile = mile ? mile : "YK8365"
    mile = this.parseMile2Int(mile)
    const gprfQuery = this.asyncLoadGeology(mile, "gprf", "construction")
    const tspfQuery = this.asyncLoadGeology(mile, "tspf", "construction")
    //const skthQuery = this.asyncLoadGeology(mile, "skth", "construction")
    const tfsiQuery = this.asyncLoadGeology(mile, "tfsi", "geology")
    const dgsrQuery = this.asyncLoadGeology(mile, "dgsr", "geology")
    const acheQuery = this.asyncLoadGeology(mile, "ache", "construction")
    var HydrologyQuery = this.asyncLoadHydrology()
    //并行执行查询，结果分别存储
    var gprf = await gprfQuery
    var tspf = await tspfQuery
    //var skth = await skthQuery
    var tfsi = await tfsiQuery //由于暂无数据，请求占用的时间过长，先行取消查询
    var dgsr = await dgsrQuery
    var Hydrology = HydrologyQuery
    var acheList = await acheQuery
    var ache = this.getache(acheList, mile)
    var stra = null
    if (dgsr.length > 0) {
      var conditionSQL = `(([STRA_GEO1]='${dgsr[0].DGSR_STRA.replace(/\s*/g, "")}'))` //
      const loadStra = this.asyncLoadGeology_SQL("geology", "STRA", conditionSQL, 1)
      stra = await loadStra
    }
    var scoreB = 0
    var catagory = 3 //风险分类，岩溶类水风险、断层类水风险和无不良地质类水风险三个大类。对应1,2,3
    var mileNum = mile.replace(/[^0-9]/ig, "")
    Hydrology.forEach(element => {
      if (element.end >= mileNum && element.start <= mileNum) {
        scoreB = element.scoreB
        catagory = element.type
      }
    })


    gprf = gprf ? gprf[0] : null
    tspf = tspf ? tspf[0] : null
    //skth = skth ? skth[0] : null
    dgsr = dgsr ? dgsr[0] : null
    tfsi = tfsi ? tfsi[0] : null
    stra = stra ? stra[0] : null
    ache = ache ? ache[0] : null
    scoreB = scoreB ? scoreB : 0
    var result = {
      "catagory": catagory,
      "scoreB": scoreB,
      "gprf": gprf,
      "tspf": tspf,
      "dgsr": dgsr,
      "tfsi": tfsi,
      "stra": stra,
      "ache": ache
      // "skth": skth,
      // "tfsi": tfsi
    }
    ////console.log("rawdata loaded" + result)
    return result
  }
  /**
   * 由于查询历史数据的过程中，默认返回的是所有里程信息，通过此函数提取对进度数据
   * @param {*} list 
   * @param {*} mile 
   */
  getache(list, mile) {
    var length = list.length
    var currentMileNum = this.getNum(mile)
    var currentPrefix = mile.substr(0, 2)
    for (let index = length - 1; index > 0; index--) {
      var progress = list[index].PROG_SGJD
      if (progress == undefined || progress == null || progress == "") { } else {
        var mileNum = this.getNum(progress)
        var diff = Math.abs(mileNum - currentMileNum)
        if (diff < 5) {
          var prefix = progress.substr(0, 2)
          if (currentPrefix == prefix) {
            return list[index]
          }
        }
      }
    }
    return null
  }
  //包装地质信息查询函数，参数含义见默认值
  asyncLoadGeology(mile, objtype, domain) {
    mile = mile ? mile : "YK8365"
    let queryModle = new DGObject({
      "ID": 0,
      "name": "test"
    }, objtype, domain)
    //便于主函数多进程操作，返回promise对象，将DGobject中的promise进一步包装
    return new Promise(function (resolve, reject) {
      queryModle.queryGeology(mile)
        .then(function (res) {
          if (res!= undefined && res.data !=null  && res.data.length > 0) {
            ////console.log(objtype + " loaded", res.data)
            // http://laoying.tongsui.com.cn/
            resolve(res.data)
          } else reject(false) //为了避免程序出错，无返回值时也resolve
        })
    }).catch((error) => {
      console.log(error);
    })
  }
  asyncLoadGeology_SQL(domain, objtype, condition, topNum) {
    let queryModle = new DGObject({
      "ID": 0,
      "name": "test"
    }, objtype, domain)
    return new Promise(function (resolve, reject) {
      queryModle.queryData(condition).then(function (res) {
        if (res!= undefined && res.data !=null  && res.data.length > 0) {
          //console.log(objtype + " loaded", res.data)
          resolve(res.data)
        } else reject(false) //为了避免程序出错，无返回值时也resolve
      }, function (error) {
        reject(false);
      })
    }).catch((error) => {
      console.log(error);
    })
  }

  /**
   * 从小程序数据库获取水文风险信息
   */
  asyncLoadHydrology() {
    // const db = wx.cloud.database()
    var data = {
      "_id": "2iD729vMYq7t6pDjm8woKe9tCkzuDu26Qw2JqFComjHicoAw",
      "data": [{
        "start": 1550,
        "end": 1700,
        "type": 1,
        "scoreB": 0
      }, {
        "start": 1700,
        "end": 2150,
        "type": 1,
        "scoreB": 0
      }, {
        "start": 2150,
        "end": 2350,
        "type": 2,
        "scoreB": 100
      }, {
        "start": 2350,
        "end": 2900,
        "type": 2,
        "scoreB": 100
      }, {
        "start": 2900,
        "end": 3100,
        "type": 2,
        "scoreB": 100
      }, {
        "start": 3100,
        "end": 3700,
        "type": 3,
        "scoreB": 0
      }, {
        "start": 3700,
        "end": 4400,
        "type": 3,
        "scoreB": 0
      }, {
        "start": 4400,
        "end": 4850,
        "type": 2,
        "scoreB": 100
      }, {
        "start": 4850,
        "end": 5050,
        "type": 2,
        "scoreB": 100
      }, {
        "start": 5050,
        "end": 5200,
        "type": 2,
        "scoreB": 100
      }, {
        "start": 5200,
        "end": 6150,
        "type": 3,
        "scoreB": 0
      }, {
        "start": 6150,
        "end": 6300,
        "type": 2,
        "scoreB": 100
      }, {
        "start": 6300,
        "end": 7700,
        "type": 2,
        "scoreB": 80
      }, {
        "start": 7700,
        "end": 8000,
        "type": 2,
        "scoreB": 80
      }, {
        "start": 8000,
        "end": 8300,
        "type": 2,
        "scoreB": 80
      }, {
        "start": 8300,
        "end": 8600,
        "type": 3,
        "scoreB": 0
      }, {
        "start": 8600,
        "end": 9300,
        "type": 2,
        "scoreB": 100
      }, {
        "start": 9300,
        "end": 9600,
        "type": 3,
        "scoreB": 0
      }, {
        "start": 9600,
        "end": 10100,
        "type": 1,
        "scoreB": 0
      }, {
        "start": 10100,
        "end": 10300,
        "type": 2,
        "scoreB": 80
      }, {
        "start": 10300,
        "end": 10750,
        "type": 3,
        "scoreB": 0
      }, {
        "start": 10750,
        "end": 10950,
        "type": 3,
        "scoreB": 0
      }, {
        "start": 10950,
        "end": 11250,
        "type": 3,
        "scoreB": 0
      }, {
        "start": 11250,
        "end": 11450,
        "type": 2,
        "scoreB": 100
      }, {
        "start": 11450,
        "end": 12650,
        "type": 2,
        "scoreB": 100
      }, {
        "start": 12650,
        "end": 12850,
        "type": 2,
        "scoreB": 100
      }, {
        "start": 12850,
        "end": 12980,
        "type": 3,
        "scoreB": 0
      }],
      "ID": 1
    }
    return data.data
  }


}
module.exports = RiskAssessor
// export {
//   RiskAssessor
// };